package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pojo.ChatHistory;
import com.ruoyi.pojo.ChatTable;
import com.ruoyi.service.ChatHistoryService;
import com.ruoyi.utils.R;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/chathistory")
@CrossOrigin
public class ChatHistoryController {

    @Autowired
    private ChatHistoryService chatHistoryService;

    @PostMapping("/addchat")
    public R<String> addChat(@RequestBody ChatHistory chatHistory){
        chatHistory.setCreatetime(new Date());
        chatHistory.setStatus("1");
        int i = chatHistoryService.addChat(chatHistory);
        System.out.println(i);
        if (i > 0) {
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }


    @GetMapping("/chatlist")
    public R<List<ChatHistory>> chatList(@RequestParam String username,@RequestParam String tousername){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("touser",username);
        queryWrapper.eq("fromuser",tousername);
        queryWrapper.or();
        queryWrapper.eq("touser",tousername);
        queryWrapper.eq("fromuser",username);
        List<ChatHistory> list = chatHistoryService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/removechat")
    public R<Boolean> removeList(@RequestBody ChatHistory chatHistory){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fromuser",chatHistory.getFromuser());
        wrapper.eq("touser",chatHistory.getTouser());
        wrapper.or();
        wrapper.eq("touser",chatHistory.getFromuser());
        wrapper.eq("fromuser",chatHistory.getTouser());
        boolean remove = chatHistoryService.remove(wrapper);
        return R.success(remove);
    }
}
